package com.dj.mingkong.portal.boot.aop;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.springframework.stereotype.Component;


/**
 * <p>
 *  日志切面
 * </p>
 * date 2021/4/27 <br/>
 * email yuan.donghao@qq.com
 *
 * @author 袁东昊
 * @version 1.0.0
 **/
@Slf4j
@Aspect
@Component
public class LogAspect {

    @Around(value = "execution(public * com.dj.mingkong.portal.biz.controller..*.*(..))")
    public Object aroundApis(ProceedingJoinPoint point) throws Throwable {
        return this.log(point);
    }

    protected Object log(ProceedingJoinPoint point) throws Throwable {
        DateTime startTime = new DateTime();
        DateTime endTime = null;
        Interval interval = null;
        Object response = null;

        try {
                log.info("请求接口 {}.{} () 参数 => {}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName(), this.argsToString(point.getArgs()));
        } catch (Exception var8) {
            log.error("请求接口 {}.{} () 参数 => {}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName(), var8);
        }

        try {
            response = point.proceed();
            endTime = new DateTime();
            interval = new Interval(startTime, endTime);
                log.info("请求接口 {}.{} () 参数 => {}, RT:{} ms, 响应 => {}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName(), this.argsToString(point.getArgs()), interval.toDurationMillis(), this.argsToString(response));

            return response;
        } catch (Exception var7) {
            endTime = new DateTime();
            interval = new Interval(startTime, endTime);
            log.error("", var7);
            log.error("请求接口 {}.{} () 参数 => {}, RT:{} ms, 响应 => {}", new Object[]{point.getSignature().getDeclaringTypeName(), point.getSignature().getName(), this.argsToString(point.getArgs()), interval.toDurationMillis(), response, var7});
            throw var7;
        }
    }


    protected String argsToString(Object object) {
        try {
            if (object instanceof Object[]) {
                StringBuilder sb = new StringBuilder();
                Object[] var3 = (Object[])object;
                int var4 = var3.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Object obj = var3[var5];
                    if (obj instanceof HttpServletRequest) {
                        HttpServletRequest requestFacade = (HttpServletRequest)obj;
                        sb.append(" request: [");
                        sb.append(" headers: ").append(JSONObject.toJSONString(requestFacade.getHeaderNames()));
                        sb.append(" parameters: ").append(JSONObject.toJSONString(requestFacade.getParameterMap()));
                        sb.append(" attribute: ").append(JSONObject.toJSONString(requestFacade.getAttributeNames()));
                        sb.append(" ] ");
                    } else if (obj instanceof HttpServletResponse) {
                        HttpServletResponse response = (HttpServletResponse)obj;
                        sb.append(" response: [");
                        sb.append(" headers: ").append(JSONObject.toJSONString(response.getHeaderNames()));
                        sb.append(" status: ").append(JSONObject.toJSONString(response.getStatus()));
                        sb.append(" attribute: ").append(JSONObject.toJSONString(response.getContentType()));
                        sb.append(" ] ");
                    } else {
                        try {
                            sb.append(JSONObject.toJSONString(obj));
                        } catch (Exception ignored) {
                        }
                    }
                }

                return sb.toString();
            } else {
                return JSONObject.toJSONString(object);
            }
        } catch (Exception var8) {
            log.error("JSON 转换出错，响应信息不明", var8);
            return "JSON 转换出错，响应信息不明";
        }
    }
}
